package com.hellogood.yaohuo.server.mgr;

import java.math.BigDecimal;
import java.util.List;

import org.apache.log4j.Logger;

import com.hellogood.yaohuo.server.DateUtil;
import com.hellogood.yaohuo.server.dao.factory.DAOFactory;
import com.hellogood.yaohuo.server.mgr.factory.MgrFactory;
import com.hellogood.yaohuo.shared.data.Constants;
import com.hellogood.yaohuo.shared.entity.Detail;
import com.hellogood.yaohuo.shared.entity.Form;
import com.hellogood.yaohuo.shared.entity.GoodsOrg;
import com.hellogood.yaohuo.shared.entity.Log;
import com.hellogood.yaohuo.shared.entity.PdForm;
import com.hellogood.yaohuo.shared.exception.DatabaseException;
import com.hellogood.yaohuo.shared.vo.MainFrame;

public class PandianMgr {
	Logger log = Logger.getLogger(this.getClass());
	private String tableFormName = "PdForm";
	private String tableDetailName = "PdDetail";
	private String billNoField = "PdBillNo";
	private String dateField = "PdDate";

	public String createPandianForm(PdForm form) {
		execute(form);
		String result = DAOFactory.getInstance().createPdFormDAO().createPandian(form, false);
		if (result.equals("")) {
			addLog(form.getBillNo(), false);
		}
		return result;
	}

	private void addLog(String billNo, boolean isModify) {
		int logID = DAOFactory.getInstance().createLogDAO().getLastLogNo();
		logID++;
		Log log = new Log();
		log.setLogId(logID);
		log.setStaffCode(MainFrame.currentUser.getStaffCode());
		log.setStaffName(MainFrame.currentUser.getStaffName());
		log.setLogDate(DateUtil.currentTime());
		if (isModify) {
			log.setContent("修改盘点单：" + billNo);
		} else {
			log.setContent("新建盘点单：" + billNo);
		}
		DAOFactory.getInstance().createLogDAO().addNewLog(log);
	}

	public String modifyPandianForm(PdForm form) {
		execute(form);
		String result = DAOFactory.getInstance().createPdFormDAO().createPandian(form, true);
		if (result.equals("")) {
			addLog(form.getBillNo(), true);
		}
		return result;
	}

	private void execute(PdForm form) {
		List<Detail> list = form.getList();
		int totalCounts = 0;
		for (Detail d : list) {
			List<GoodsOrg> goodsOrgs = MgrFactory.getInstance().createGoodsOrgMgr().getGoodsOrgByCode(d.getPluCode(), Constants.C001);
			BigDecimal jj = new BigDecimal(0);
			BigDecimal sj = new BigDecimal(0);
			if (goodsOrgs.size() > 0) {
				GoodsOrg go = goodsOrgs.get(0);
				jj = go.getjPrice();
				sj = go.getPrice();
			}
			d.setjPrice(jj);
			d.setPrice(sj);
			d.setCounts(d.getCounts());
			totalCounts += d.getCounts();
		}
		form.setCounts(totalCounts);
	}

	public int getLastPdBillNo(String billNo) {
		String no = DAOFactory.getInstance().createPdFormDAO().getLastBillNo(billNo, tableFormName, billNoField);
		if (no != null) {
			// 序号为最后两位 格式为"WLYH组织号YYYYMMDDXX" XX为序号
			if (no.length() > 4) {
				String lastNo = no.substring(no.length() - 4, no.length());
				return Integer.valueOf(lastNo);
			}
		}
		return 0;

	}

	public List<Form> searchPdForm(String code, int level, String startDate, String endDate, String rz) {

		if (code.equals("0")) {
			return DAOFactory.getInstance().createPdFormDAO().searchFormByQuyu(tableFormName, billNoField, dateField, null, startDate, endDate, rz);
		} else {
			return DAOFactory.getInstance().createPdFormDAO().searchForm(tableFormName, billNoField, dateField, code, startDate, endDate, rz);
		}
	}

	public Form getFormDetail(String billNo) {
		Form form = DAOFactory.getInstance().createPdFormDAO().getFormByBillNo(tableFormName, billNoField, dateField, billNo);
		if (form != null) {
			List<Detail> list = DAOFactory.getInstance().createPdDetailDAO().getDetailByBillNo(billNo, tableDetailName, billNoField);
			form.setList(list);
		}
		return form;
	}

	public String deletePandian(String billNo) {
		return DAOFactory.getInstance().createPdFormDAO().deleteForm(tableFormName, tableDetailName, billNoField, billNo);
	}

	public String rz(List<String> bills) {
		String result = "";
		try {
			for (String bill : bills) {
				String temp = DAOFactory.getInstance().createPdFormDAO().rz(bill);
				if (!temp.equals("")) {
					result = temp;
					break;
				}
			}
		} catch (DatabaseException e) {
			log.error(e.getMessage());
			e.printStackTrace();
			result = e.getMessage();
		}
		return result;
	}
}
